var argv = process.argv.splice(2);
var version = argv[0];
var serverType = argv[1];
var servers = {
    1: {
        name: "内网测试服",
        host: "192.168.20.156",
    },
    2: {
        name: "外网测试服",
        host: "show.ky34.com",
    },
    3: {
        name: "正式服",
        host: "ws.ky34.com",
    },
     4: {
        name: "演示服",
        host: "demo.ky34.com",
    },
};
if (!version) {
    console.log("请输入版本号");
    process.exit(0);
}
if (!serverType || !servers[serverType]) {
    console.log("请输要发布的服务器:");
    for (var key in servers) {
        console.log(key + ":" + servers[key].name + " " + servers[key].host);
    }
    process.exit(0);
}
var os = require('os');
var fs = require('fs');
var path = require('path');
var readline = require('readline');
var defaultPath = "./bin-release/web/" + version;
var defaultConfigPath = "./src/config/GameConfig.ts";
console.log("开始发布 版本： " + version + " 服务器：" + servers[serverType].name + " " + servers[serverType].host);
console.log("更换配置文件");
var fWrite = fs.createWriteStream(path.resolve(__dirname, './src/config/GameConfig.ts'));
var objReadline = readline.createInterface({ input: fs.createReadStream(path.resolve(__dirname, './releaseTool/config/GameConfig' + serverType)) });
objReadline.on('line', (line) => {
    if (line.indexOf("version") != -1) {
        line = '    public static version: string = "' + version + '"';
    }
    fWrite.write(line + os.EOL); // 下一行
});
objReadline.on('close', () => {
    var exec = require('child_process').exec;
    var child = exec('egret publish gamehall --version ' + version + ' --runtime html5');
    child.stdout.on('data', function (data) { console.log(data); });
    child.on('exit', function (code) {
        var fWrite = fs.createWriteStream(path.resolve(__dirname, defaultPath + '/index1.html'));
        var objReadline = readline.createInterface({ input: fs.createReadStream(path.resolve(__dirname, defaultPath + '/index.html')) });
        objReadline.on('line', (line) => {
            if (line.indexOf("script") != -1 && line.indexOf("src") != -1) {
                var index = line.indexOf('.js');
                if (line.indexOf("?v=") == -1) {
                    var str1 = line.substring(0, index + 3);
                    var str2 = line.substring(index + 3, line.length);
                    line = str1 + "?v=" + version + str2;
                }
            }
            if (line == "</body>" && serverType == 3) {
                line = '   <script>'
                    + 'var _hmt = _hmt || [];'
                    + '(function () {'
                    + 'var hm = document.createElement("script");'
                    + 'hm.src = "https://hm.baidu.com/hm.js?a3415bedf95a95d65135351b7e7fbfcc";'
                    + 'var s = document.getElementsByTagName("script")[0];'
                    + 's.parentNode.insertBefore(hm, s);'
                    + '})();'
                    + '</script>' + os.EOL + line;
            }
            fWrite.write(line + os.EOL); // 下一行
        });
        objReadline.on('close', () => {
            var defaultRes = require(path.resolve(__dirname, defaultPath + "/resource/default.res.json"));
            for (var i = 0; i < defaultRes.resources.length; i++) {
                var url = defaultRes.resources[i].url;
                if (url.indexOf("?v=") == -1) {
                    defaultRes.resources[i].url = url + "?v=" + version;
                }
            }
            fs.writeFileSync(path.resolve(__dirname, defaultPath + '/resource/default.res.json'), JSON.stringify(defaultRes));
            fs.unlinkSync(path.resolve(__dirname, defaultPath + '/index.html'));
            fs.rename(path.resolve(__dirname, defaultPath + '/index1.html'), path.resolve(__dirname, defaultPath + '/index.html'), function (err) {
                if (err) {
                    console.log("版本发布失败！");
                } else {
                    console.log("发布成功 web 版本：" + version);
                    console.log("还原配置文件");
                    fs.writeFileSync(path.resolve(__dirname, defaultConfigPath),
                        fs.readFileSync(path.resolve(__dirname, './releaseTool/config/GameConfig0')));
                    exec('explorer.exe ' + path.resolve(__dirname, defaultPath));
                }
            });
        });
    });
});

